package org.rednebula.framework.core.model.domain;

import java.io.Serializable;

import org.rednebula.framework.core.model.State;

public class StateChangeRule implements Serializable  {
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static	StateChangeRule rule=new StateChangeRule();
   public static StateChangeRule getInstance(){
	   return rule;
   }
   private StateChangeRule(){}
   public State change(State to,State from){
	   State v=to;
	   if(State.CREATE ==to){
		   v=changeCreate(from);
	   }else if(State.DELETE == to){
		   v=changeDelete(from);
	   }else if(State.MODIFY == to){
		   v=changeModify(from);
	   }else if(State.NORMAL == to){
		   v=changeNormal(from);
	   }else if(State.TRANSIENT == to){
		  v=changeTransient(from);
	   }
	   
	   return v;
   }
   
   /**
    * 改变当前状态到  normal 状态的，规则
    * 
    *  1. transient 不能改为 normal 状态
    *  2. delete  不能改为 normal 状态
    * @param from
    * @return
    */
   private State changeNormal(State from){
	   State v=State.NORMAL;
	   if(State.TRANSIENT==from){
		   v=from;
	   }else if(State.DELETE == from){
		   v=from; 
	   }
	   return v;
   }
   
   /**
    * 改变当前状态到  create 状态的，规则
    * 
    *  1. transient 不能改为 create 状态
    *  2. delete  不能改为 create 状态
    *  3. normal  不能改为 create 状态
    * @param from
    * @return
    */
   private State changeCreate(State from){
	   State v=State.CREATE;
	   if(State.TRANSIENT==from){
		   v=from;
	   }else if(State.DELETE == from){
		   v=from; 
	   }else if(State.NORMAL == from ){
		   v=from;
	   }
	   return v;
   }
   
   /**
    * 改变当前状态到  modify 状态的，规则
    * 
    *  1. transient 不能改为 MODIFY 状态
    *  2. delete  不能改为 MODIFY 状态
    *  3. CREATE  不能改为 MODIFY 状态
    * @param from
    * @return
    */
   private State changeModify(State from){
	   State v=State.MODIFY;
	   if(State.TRANSIENT==from){
		   v=from;
	   }else if(State.DELETE == from){
		   v=from; 
	   }else if(State.CREATE == from ){
		   v=from;
	   }
	   return v;
   }
   
   /**
    * 改变当前状态到  DELETE 状态的，规则
    * 
    *  1. transient 不能改为 DELETE 状态
    *  2. CREATE  要改为 transient 状态

    * @param from
    * @return
    */
   private State changeDelete(State from){
	   State v=State.DELETE;
	   if(State.TRANSIENT==from){
		   v=from;
	   }else if(State.CREATE == from ){
		   v=State.TRANSIENT;
	   }
	   return v;
   }
   
   /**
    * 改变当前状态到  TRANSIENT 状态的，规则
    * @param from
    * @return
    */
   private State changeTransient(State from){
	   return State.TRANSIENT;
   }
}
